package com.jml.test.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;


//身份验证
public class LoginTextDemo {
    //文本中读取用户名密码
    public static void main(String[] args) {
        //读取配置文件，初始化SecurityManager工厂
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        //获取securityManager实例
        SecurityManager securityManager = factory.getInstance();
        //把securityManager实例绑定到SecurityUtils
        SecurityUtils.setSecurityManager(securityManager);
        //得到当前执行的用户
        Subject currentUse = SecurityUtils.getSubject();
        //创建token令牌，用户名密码
        UsernamePasswordToken token = new UsernamePasswordToken("jml","123");
        //身份认证
        try {
            currentUse.login(token);
            System.out.println("login successful!!");
        } catch (AuthenticationException e) {
            e.printStackTrace();
            System.out.println("login failed!!");
        }
        currentUse.logout();
    }
}
